package com.heo.web.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.heo.web.entity.Device;
import com.heo.web.entity.KaoLaDeviceInfo;
import com.heo.web.service.HttptDeviceService;
import com.heo.web.util.Constants;
import com.heo.web.util.GsonUtils;
import com.heo.web.util.RedisUtil;
import com.heo.web.util.Utils;

@Service
@Transactional(readOnly = true)
public class HttptDeviceServiceImpl implements HttptDeviceService {

	@Autowired
	private RedisUtil redisUtill;

	
	public Map<String, Object> deviceController(Device device) throws Exception {
		Map<String, Object> rspJson = new HashMap<String, Object>();
		// 设备状态，0：低电频；1：高电频3:表示已经发送脉冲
		int status = device.getS();
		String macAddress = device.getM();
		int vs = Utils.getInt(device.getV()); // 版本号参数
		// 如果有故障码 那么优先处理故障码
		String ec = device.getE();
		KaoLaDeviceInfo kaoLaDeviceInfo = null;
		String jsonString =  (String) redisUtill.getValue(macAddress);
		 kaoLaDeviceInfo = (KaoLaDeviceInfo) GsonUtils.jsonToObject(jsonString, KaoLaDeviceInfo.class);
		if (kaoLaDeviceInfo == null ) {
			return paramsMap(rspJson, -2, 600);
		}

		//Long deviceStatus = Utils.getLong(washerInfo.get("WASHER_STATUS"));
		//Map<String, Object> dockingSetMap = cache.getDockingSet();

		// 修改设备iccid号码
		String iccid = device.getI();
		if (Utils.checkNull(iccid)) {
			long washerId = Utils.getLong(kaoLaDeviceInfo.getWasherId());
			/*//updateWasherElectricCodeId(washerId, iccid);
			// 初始化洗衣工作
			Map<String, Object> washingInfos = washDao.getWashingInfo(macAddress);
			if (washingInfos != null && washingInfos.size() > 0) {
				int finishStatus = Utils.getInt(washingInfos
						.get("FINISH_STATUS"));
				long washingIds = Utils.getLong(washingInfos
						.get("WASHING_ID"));
				if (finishStatus == 3) {
					// 如果设备状态在工作中，那么改变状态为 空闲
					//updateDeviceStatus(macAddress,Constants.WASHDEVICE_STATUS_ZORE, washingIds,Constants.WASH_STATUS_ZORE);
				}
			} else {
				// 投币工作 结束,空闲中
				//updateWashStatus(macAddress, Constants.WASHDEVICE_STATUS_ZORE);
			}*/
			/**
			 * d参数 CCCCCP CCCCC：脉冲( 00050=表示50ms脉冲，00100=表示100ms脉冲)
			 * P：电路接口模式，0=表示通用接口，1=特殊洗衣机接口 000500：表示50ms脉冲模式，0表示http 1表示tcp
			 * 表示投币模式,0表示硬件接口模式,1表示通讯接口模式
			 * 
			 * CCCCCPO    CCCCC：脉冲( 00050=表示50ms脉冲，00100=表示100ms脉冲)   
			 * P：处理模式，0=通用TCP短连接，1=TCP长连接    O：投币模式，0=使用硬件接口，1=使用通讯接口（硬件投币保持生效）
			 */
			int deviceType = Utils.getInt(kaoLaDeviceInfo.getDeviceType());
			int pulseWidth = Utils.getInt(kaoLaDeviceInfo.getPulseWidth());
			StringBuilder dSb = new StringBuilder("00");
			if (pulseWidth > 0) {
				if (pulseWidth < 100) {
					dSb.append("0").append(pulseWidth);
				} else {
					dSb.append(pulseWidth);
				}
			} else {
				dSb.append("045");
			}

			rspJson.put("l", 180);
			if (vs > 1) {
				//rspJson.put("s", dockingSetMap.get("NEXT_REQ_IP")); 
				//rspJson.put("p", dockingSetMap.get("NEXT_SOCKET_PORT")); 

				// XXX三个参数详解 第一个X 1表示长连接tcp 第二个X，0表示串口模式，1表示通讯模式 第三个X 0表示单投币
				// 1表示循环投币 2表示透传模式
				// deviceType 详解 应用模式，0原始 投币(带启动检测类) 1 通讯投币(带启动检测类) 2原始 
				// 投币(循环投币类) 3 通讯投币(循环投币类) 4 透传模式(带启动检测类)，5 透传模式(循环投币类)
				int terminalType = Utils.getInt(kaoLaDeviceInfo.getDeviceType());
				if(terminalType == 8){
					rspJson.put("p", "8793");
					dSb.append("112");
				}else if (deviceType == 0) {
					dSb.append("100");
				} else if (deviceType == 1) {
					dSb.append("110");
				} else if (deviceType == 2) {
					dSb.append("101");
				} else if (deviceType == 3) {
					dSb.append("111");
				} else if (deviceType == 4 || deviceType == 5 || deviceType == 7 || deviceType == 8 || deviceType == 9 || deviceType == 10 || deviceType == 11 || deviceType == 12 || deviceType == 13 || deviceType == 14) {
					rspJson.put("p", "8796");
					dSb.append("112");
				}

				rspJson.put("d", dSb.toString());
				rspJson.put("t", 10);
				// rspJson.put("s", dockingSetMap.get("NEXT_REQ_IP")); //
				// rspJson.put("p", dockingSetMap.get("NEXT_SOCKET_PORT")); //
				// Utils.getInteger()
				rspJson.put("d", dSb.toString());
				//int proxyXz = Utils.getInt(washerInfo.get("PROXY_XIAOZHANG"));
				//if(proxyXz == 1){
					String ip = Utils.getStr(kaoLaDeviceInfo.getProxyIp());
					String port = Utils.getStr(kaoLaDeviceInfo.getProxyPort());
					if(Utils.checkNull(ip) && Utils.checkNull(port)) {
						rspJson.put("s", ip); 
						rspJson.put("p", port); 
						return rspJson;
					}
				//}
				
			}
			if (deviceType == 1) {
				dSb.append("11");
			} else {
				dSb.append("10");
			}

			
		}
		return rspJson;
	}


	private Map<String, Object> paramsMap(Map<String, Object> rspJson, int cmd,
			int time) {
		//Map<String, Object> dockingSetMap = cache.getDockingSet();
		if (cmd != -2) {
			rspJson.put("c", cmd);
		}

		String macAddress = Utils.getStr(rspJson.get("m"));
		//String macCmd = cache.getMacAddress(macAddress);
		/*if (Utils.checkNull(macCmd)) {
			int macCmds = Utils.getInteger(macCmd);
			if (macCmds != -1) {
				rspJson.put("g", macCmds);
			}
			rspJson.put("c", macCmds);
		}*/
		rspJson.put("t", time);
		// rspJson.put("s", dockingSetMap.get("NEXT_REQ_IP")); //
		// rspJson.put("p", dockingSetMap.get("NEXT_REQ_PORT")); //
		// Utils.getInteger()
		rspJson.put("s", "8zao.net");
		rspJson.put("p", "9999");
		//rspJson.put("u", dockingSetMap.get("NEXT_REQ_PATH")); // dockingSetMap.get("NEXT_REQ_PATH")
		//rspJson.put("i", dockingSetMap.get("HTTP_TIME_OUT"));
		return rspJson;
	}

}
